package com.springdemo.mapper;

import com.springdemo.pojo.Book;
import com.springdemo.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
@Mapper
public interface UserMapper {
    @Select("select * from user")
    List<User> findAll();
    @Delete("delete from user where id = #{id}")
    boolean deleteUser(Integer id);
    @Select("select * from user where name = #{name}")
    List<User>  findUserByName(String name);

    @Select("select * from user where phone = #{phone}")
    List<User>  findUserByPhone(String phone);
    @Select("select * from user where book_name = #{bookName}")
    List<User>  findBookName(String bookName);

    @Insert("INSERT INTO user (id, name, phone, book_name, borrow_date, return_date) " +
            "VALUES (#{id}, #{name}, #{phone}, #{bookName}, #{borrowDate}, #{returnDate})")
    boolean addUser(User user);

    @Update("UPDATE user " +
            "SET name = #{name}, phone = #{phone}, book_name = #{bookName}, " +
            "borrow_date = #{borrowDate}, return_date = #{returnDate} " +
            "WHERE id = #{id}")
    boolean updateUser(User user);
    @Insert("INSERT INTO book (id, book_name, author, number) " +
            "VALUES (#{id}, #{bookName}, #{author}, #{number})")
    boolean addBook(Book book);

    @Select("SELECT number FROM book WHERE book_name = #{bookName}")
    int findBookNumber(String bookName);

    @Update("UPDATE book SET number = number - 1 WHERE book_name = #{bookName}")
    boolean updateBook(String bookName);
}
